#include<bits/stdc++.h>
using namespace std;


struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* mov1 = l1, *mov2 = l2, *new_node, *ans = new ListNode(0);
        //先把两个数字位数补齐
        while(mov1->next || mov2->next){
            if(mov1->next && !mov2->next){
                mov2->next = new ListNode(0);
            }
            else if(!mov1->next && mov2->next){
                mov1->next = new ListNode(0);
            }
            mov1 = mov1->next;
            mov2 = mov2->next;
        }
       
        mov1 = l1;
        mov2 = l2;
        new_node = ans;
        int carry = 0;
        while(mov1 && mov2){
            new_node->next = new ListNode((mov1->val + mov2->val + carry) % 10);
            new_node = new_node->next;
            carry = (mov1->val + mov2->val + carry) / 10;
            mov1 = mov1->next;
            mov2 = mov2->next;
        }
        if(carry){
            new_node->next = new ListNode(1);
        }
        return ans->next;
    }
};
/**
 * @brief 2022.5.5
 * 先对齐位数，再各位相加
 * 
 */